/**
 * 宽带开户公共JS
 */
var NumCode = "NA";
var ChannelType = "NA";
var prodId = "";//宽带产品标示，对应开户接口中的prod_id字段
var prodPrcid = "";//宽带产品资费代码，对应开户接口中的prod_prcid字段
var business_code = "";//套餐资费代码

var closeLoadingModal = function() {
  $('#modal').modal('close');
};

var showLoadingModal = function(msg) {
  if(msg){
    $('#modal .am-modal-hd').html(msg);
  }

  $('#modal').modal('open');
};

var showAlertMessage = function(message) {
  $("#alert-content").html(message);
  $("#alert").modal('open');
};

var checkDomainResult=function(outData){
  if(!outData){
	  showAlertMessage("查询数据为空");
	  return;
  }   
  var bdParams = getParams() + "&NumberCode=" + NumCode + "&ChannelType=" + ChannelType;
  bdParams += "&enter_num="+outData.ENTER_NUM + "&domains=" + outData.DOMAINS + "&band_width=" + outData.BAND_WIDTH + "&prepay_fee=" + outData.PRE_PAY_FEE + "&enter_fee=" + outData.ENTER_FEE + "&ma_fee=" + outData.MA_FEE;
  bdParams += "&business_code=" + business_code + "&bd_id="+$("#broadband-id").val()+"&BdName=" + $("#broadband-name").html() + "&prod_id=" + prodId + "&prod_prcid=" + prodPrcid;
  window.location.replace("broadband_create_order.html?" + bdParams);
};

/*
加载数据
*/
var doSRelChk = function(businessCcode) {
  var url = Constants.apiV2HostRest + "s1093GetPrcInfo";
  var data = {
    "BODY": {
        "PHONE_NO": getParamValue("phonenumber"),
        "LOGIN_NO": getParamValue("EmpCode"),
        "GROUP_ID": getParamValue("group_id"),
        "PROD_PRCID": businessCcode,
        "OP_CODE": "1093",
        "OP_NOTE": ""
    }
  };  
  console.log("esb.doSRelChk.url=" + url);
  console.log("esb.doSRelChk.data=" + JSON.stringify(data));
  $.ajax({
    type: "post",
    url: url,
    data: JSON.stringify(data),
    contentType: "application/json",
    dataType: "json",
    headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
      closeLoadingModal();
      pageData = data.ROOT;
      console.info("esb.data:" + JSON.stringify(data));
      if(data.ROOT.RETURN_CODE  != 0){
        showAlertMessage(data.ROOT.USER_MSG);
        return;
      }
      checkDomainResult(data.ROOT.OUT_DATA);
    },
    error: function(textStatus) {
      closeLoadingModal();
      showAlertMessage("网络失败，请稍后重试！");
      console.info("request error:" + textStatus);
    }
  });
};

/**
解析宽带产品标示数据
*/
var setDatas = function(data){
  if(data){
    var row = data.ROW;
    if(row){
      var rowArr = new Array();
      if(row instanceof Array){
        rowArr = row;
      }else if(row instanceof Object){
        rowArr.push(row);
      }

      for(var i=0;i<rowArr.length;i++){
        row = rowArr[0];
        if(row){
          var rowValue = row.CODE_VALUE;
          var rowValueArr = rowValue.split("|");
          if(rowValueArr && rowValueArr.length == 3){
            prodId = rowValueArr[0];
            prodPrcid = rowValueArr[1];
            console.log("searchProductMark prodId = "+prodId);
            console.log("searchProductMark prodPrcid = "+prodPrcid);
          }
        }
      }
    }
  }
};

/**
宽带产品标示数据正确则提交开户信息
*/
var showProductMark = function(data){
  setDatas(data);

  if(!prodPrcid || !prodId){
    closeLoadingModal();
    showAlertMessage("无资费对应关系，请联系地市业支配置！");
  }else{
    doSRelChk(prodPrcid);
  }
};

var setCommonValue = function(businessCode,isList){
  business_code = businessCode;
  if(!isList){
    NumCode = getParamValue("NumberCode");
    ChannelType = getParamValue("ChannelType");
  }
};

/**
  宽带产品标示查询
*/
var searchProductMark = function(businessCode,isList){
  console.log("businessCode="+businessCode);
  setCommonValue(businessCode,isList);
  showLoadingModal("提交中...");
  setCommonValue(businessCode,isList);
  var url = Constants.apiV2HostRest + "sDynSvc";
  var data = {
    "BODY": {
      "PHONE_NO": getParamValue("phonenumber"),
      "SVC_NAME": "d1093B0003",
      "GROUP_ID": getParamValue("group_id"),
      "INPUT_CODE": businessCode,
      "LOGIN_NO":getParamValue("EmpCode"),
      "TYPE_CODE_OTHER":"12"
    }
  };
  console.log("searchProductMark url=" + url);
  $.ajax({
    type: "post",
    url: url,
    data: JSON.stringify(data),
    contentType: "application/json",
    dataType: "json",
    headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
      console.log("data:" + JSON.stringify(data));
      if(data.ROOT.RETURN_CODE != 0){
        closeLoadingModal();
        showAlertMessage(data.ROOT.USER_MSG);
        return;
      }
      showProductMark(data.ROOT.OUT_DATA);
    },
    error: function(textStatus) {
      closeLoadingModal();
      showAlertMessage("网络失败，请稍后重试！");
      console.info("request error:" + textStatus);
    }
  });
};